﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZSZ.CommonMVC;
using ZSZ.DTO;
using ZSZ.IService;
using ZSZ.Model;
using ZSZ.Website.Areas.WebAdmin.Filters;
using ZSZ.Website.Areas.WebAdmin.Models;

namespace ZSZ.Website.Areas.WebAdmin.Controllers
{
    /// <summary>
    /// 小区
    /// </summary>
    public class CommunityController : Controller
    {
        // GET: WebAdmin/Community
        public ICityService cityService { get; set; }
        public IRegionService regionService { get; set; }

        public ICommunityService communityService { get; set; }

        [AdminAuthorize(IsCheck = true)]
        public ActionResult List(CommunitySearchOptions options, int pageIndex = 1)
        {
            #region 获取所有城市
            List<SelectListItem> listCitiy = new List<SelectListItem>();
            //listCitiy.Add(new SelectListItem { Value = "", Text = "所有城市" });
            var allCities = cityService.GetAll().ToArray();
            foreach (var city in allCities)
            {
                listCitiy.Add(new SelectListItem { Value = city.Id.ToString(), Text = city.Name });
            }
            //获取所有城市
            ViewBag.ListCitiy = listCitiy; 
            #endregion

            long total;
            int pageSize = 10, maxPagerCount = 10;
            List<CommunityDTO> list = communityService.GetPagedData(options, out total, pageIndex, pageSize);
            ViewBag.totalCount = total;
            ViewBag.pageIndex = pageIndex;
            ViewBag.pageSize = pageSize;
            ViewBag.maxPagerCount = maxPagerCount;
            return View(list);
        }

        [HttpGet]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Add()
        {
            #region 获取所有城市
            List<SelectListItem> listCitiy = new List<SelectListItem>();
            var allCities = cityService.GetCities();
            foreach (var city in allCities)
            {
                listCitiy.Add(new SelectListItem { Value = city.Id.ToString(), Text = city.Name });
            }
            //获取所有城市
            ViewBag.ListCitiy = listCitiy;
            #endregion

            return View();
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Add(string name,long regionId,string location,string traffic,int? builtYear)
        {
            var region = regionService.GetById(regionId);
            if (region == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "区域不存在" });
            }
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "小区名称不能为空" });
            }
            if (string.IsNullOrEmpty(location))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "地址不能为空" });
            }

            //新增区域
            communityService.Add( name,  regionId,  location,  traffic,  builtYear);

            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpGet]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Edit(long id)
        {
            CommunityDTO entity = communityService.GetDTOById(id);
            if (entity == null)
            {
                return View("Error", (object)"获取不到小区信息");
            }

            #region 获取所有城市，并选中
            List<SelectListItem> listCitiy = new List<SelectListItem>();
            var allCities = cityService.GetCities();
            foreach (var city in allCities)
            {
                if (city.Id == entity.CityId)
                {
                    listCitiy.Add(new SelectListItem { Value = city.Id.ToString(), Text = city.Name, Selected = true });
                }
                else
                {
                    listCitiy.Add(new SelectListItem { Value = city.Id.ToString(), Text = city.Name });
                }
            }
            ViewBag.ListCitiy = listCitiy;
            #endregion

            #region 获取所有区域，并选中
            RegionDTO[] regions = regionService.GetByCityId(entity.CityId);
            List<SelectListItem> listRegion = new List<SelectListItem>();
            foreach (RegionDTO region in regions)
            {
                if (region.Id== entity.RegionId)
                {
                    listRegion.Add(new SelectListItem { Value = region.Id.ToString(), Text = region.Name, Selected = true });
                }
                else
                {
                    listRegion.Add(new SelectListItem { Value = region.Id.ToString(), Text = region.Name });
                }
            }
            ViewBag.ListRegion = listRegion;
            #endregion

            return View(entity);
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Edit(long id, string name, long regionId, string location, string traffic, int? builtYear)
        {
            Community community = communityService.GetById(id);
            if (community == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "获取不到小区信息" });
            }
            var region = regionService.GetById(regionId);
            if (region == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "区域不存在" });
            }
            if (string.IsNullOrEmpty(name))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "小区名称不能为空" });
            }
            if (string.IsNullOrEmpty(location))
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "地址不能为空" });
            }

            //更新区域
            communityService.Update(id, name, regionId, location, traffic, builtYear);

            return Json(new AjaxResult { Status = "ok" });
        }


        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult Delete(long id)
        {
            CommunityDTO entity = communityService.GetDTOById(id);
            if (entity == null)
            {
                return View("Error", (object)"获取不到小区信息");
            }
            communityService.MarkDeleted(id);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        [AdminAuthorize(IsCheck = true)]
        public ActionResult BatchDelete(long[] selectdIds)
        {
            List<long> list = new List<long>();
            foreach (long id in selectdIds)
            {
                list.Add(id);
            }
            communityService.BatchMarkDeleted(list);
            return Json(new AjaxResult { Status = "ok" });
        }

        [HttpPost]
        public ActionResult LoadRegions(long cityId)
        {
           RegionDTO[] regions = regionService.GetByCityId(cityId);
            var data = regions.Select(r=> new { id=r.Id,name=r.Name });
            return Json(new AjaxResult { Status = "ok",Data= data });
        }
    }
}